//
//  GLGWRequestTool.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/9.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "GLGWRequestTool.h"
#import "GLGWInterfacedConst.h"
#import "PPNetworkHelper.h"

@implementation GLGWRequestTool
#pragma mark - 请求的公共方法
+(NSURLSessionTask *)POST_C:(NSString *)URL
                 lodingView:(UIView * __nullable)l_View
           lodingTextString:(NSString * __nullable)lodingStr
                   isLoding:(BOOL)loding
                 parameters:(id)parameters
              responseCache:(GLGWHttpRequestCache)responseDataCache
                    success:(GLGWRequestSuccess)success
                    failure:(GLGWRequestFailure)failure{
    GLGWProgressHUD *HUD;
    if (loding) {
        HUD = [GLGWProgressHUD drawRoundLoadingView:@"请稍后..." view:nil];
    }
    [PPNetworkHelper setRequestSerializer:PPRequestSerializerJSON];
    return [PPNetworkHelper POST:URL parameters:parameters responseCache:^(id responseCache) {
        responseDataCache(responseCache);
    } success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
}

+(NSURLSessionTask *)POST:(NSString *)URL
               lodingView:(UIView *__nullable)l_View
         lodingTextString:(NSString *__nullable)lodingStr
                 isLoding:(BOOL)loding
               parameters:(id)parameters
                  success:(GLGWRequestSuccess)success
                  failure:(GLGWRequestFailure)failure{
    GLGWProgressHUD *HUD;
    if (loding) {
        HUD = [GLGWProgressHUD drawRoundLoadingView:@"请稍后..." view:l_View];
    }
    if (parameters) {
        DLog(@"请求数据----%@----\n%@",URL,[YLJ_StringTool dictionaryToJson:parameters]);
    }
    [PPNetworkHelper setRequestSerializer:PPRequestSerializerJSON];
    return [PPNetworkHelper POST:URL parameters:parameters success:^(id responseObject) {
        [HUD hide:YES];
        DLog(@"返回数据----%@----\n%@",URL,[YLJ_StringTool dictionaryToJson:responseObject]);
        success(responseObject);
    } failure:^(NSError *error) {
        DLog(@"错误提示----%@----\n%@",URL,error);
        [HUD hide:YES];
        failure(error);
    }];
}

+(NSURLSessionTask *)uploadImagesWithURL:(NSString *)URL
                              lodingView:(UIView *)l_View
                        lodingTextString:(NSString *)loding_Str
                              parameters:(id)parameters
                                    name:(NSString *)name
                                  images:(NSArray<UIImage *> *)images
                               fileNames:(NSArray<NSString *> *)fileNames
                              imageScale:(CGFloat)imageScale
                               imageType:(NSString *)imageType
                                progress:(GLGWRequestProgress)myProgress
                                 success:(GLGWRequestSuccess)success
                                 failure:(GLGWRequestFailure)failure{
    GLGWProgressHUD *HUD = [GLGWProgressHUD drawRoundLoadingView:@"请稍后..." view:l_View];
    [PPNetworkHelper setRequestSerializer:PPRequestSerializerJSON];
    return [PPNetworkHelper uploadImagesWithURL:URL parameters:parameters name:name images:images fileNames:fileNames imageScale:imageScale imageType:imageType progress:^(NSProgress *progress) {
        myProgress(progress);
    } success:^(id responseObject) {
        [HUD hide:YES];
        success(responseObject);
    } failure:^(NSError *error) {
        [HUD hide:YES];
        failure(error);
    }];
}

@end
